package com.amado.course49AnonymousClass.zuoye;

import com.amado.course49AnonymousClass.Student;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * compare(Object o1, Object o2) {
 * 	return -1;  //返回负数时参数的顺序就是排的顺序即:o1,o2
 * }
 *
 * compare(Object o1, Object o2) {
 * 	return 1;  //返回正数时参数的反序就是排的顺序即:o2,o1
 * }
 *
 * 假设有个person类，有年龄属性age
 *
 *compare(Person p1,Person p2){
		 *return p1.age-p2.age;
		 *    //如果p1.age > p2.age 返回正数，排序结果为：p2,p1
		 *    //如果p1.age < p2.age 返回负数，排序结果为：p1,p2
		 *    //即：从小到大排序，升序排序
		 *    //同理，return p2.age - p1.age 为从大到小排序。降序排序。
		 *}
 * @param <T>
 */
public class Comparetor2Test<T> {
	static List<Student> studentList = new ArrayList();

	public static void main(String[] args) {
		Comparetor2Test<Student> test = new Comparetor2Test();
		test.add(new Student("张三", 60,100));
		test.add(new Student("李四", 20,120));
		test.add(new Student("张三", 41,140));
		test.add(new Student("王五", 11,200));
		test.add(new Student("王五", 91,100));
		/**
		 * a negative integer, zero, or a positive integer as the
		 * first argument is less than, equal to, or greater than the
		 * second.
		 *
		 * o1与o2比较 于小返咽负数，==返回0，大于返回正数
		 * 返回负数换顺序
		 */
		studentList.sort(new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				int value =  o2.getAge()-o1.getAge();
				System.out.println(o1+"  "+o2+" value "+value);
				return value;
			}
		});
		System.out.println("--------------");
		studentList.sort(new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				int value =  o2.getAge()-o1.getAge();
				System.out.println(o1+"  "+o2+" value "+value);
				return value;
			}
		});
		System.out.println("----------------");
		test.print();
		
	}

	public void add(Student t) {
		studentList.add(t);
	}


	public void print() {
		for (Student t:studentList) {
			System.out.println(t);
		}
	}
}
